home *** CD-ROM | disk | FTP | other *** search
- /* GadMGet.c by Jeremy Friesner -- jfriesne@ucsd.edu
- Version 1.0, released 09 June 1994 */
-
- #ifndef MGET_C
- #define MGET_C
-
- #define INTUI_V36_NAMES_ONLY
- #define NAMELENGTH 81
- #define MAXFILELINELENGTH 150
- #define CHAR_SPACE 32
- #define MAXOUTPUTLINELENGTH 254
-
- #include "GadMget.h"
-
- extern UWORD lastcode;
- extern struct NewGadget mgetNGad[];
- int IntuitionBase;
- ULONG winopenerror, lockscrnerror;
- ULONG handlereturn;
- BOOL done=FALSE;
- char windowtitle[75];
- char version[] = "$VER: GadMget V1.0";
-
- struct Library *Intuitionbase;
- struct GraphicsLib *GraphicsBase;
- struct Library *GadToolsBase;
- struct Library *UtilityBase;
-
- struct IntuiMessage *message = NULL;
- struct List *filelist, *getlist;
-
- char inputfile[50];
- int files_selected = 0, screenisopen = 0, windowisopen = 0;
- int noparse = 0, nomget = 0, nosplit = 0;
- ULONG kbytes_selected = 0;
-
- struct EasyStruct aboutreq =
- {
- sizeof(struct EasyStruct),
- 0,
- "GadMget 1.0",
- "\nGadMget 1.0\n\nby Jeremy Friesner\njfriesne@ucsd.edu\n",
- "Ok",
- };
-
- void debug(int a)
- {
- printf("debug %i\n",a);
- Delay(40);
- }
-
- int listviewgadgetClicked (VOID)
- {
- int i;
- struct Node *current = filelist->lh_Head;
- struct Gadget *toplist = mgetGadgets[0];
- struct Gadget *botlist = mgetGadgets[1];
- char *dataline = NULL;
- int oktosend = 1;
-
- for (i=0; i<lastcode; i++)
- {
- current = current->ln_Succ;
- }
-
- if (current != NULL)
- {
- /* check to make sure string is a valid file line */
- dataline = current->ln_Name;
- if (dataline == NULL) oktosend = 0;
- if (strlen(current->ln_Name) <2) oktosend = 0;
-
- if (!noparse)
- {
- if ((dataline[0] < '0')&&(dataline[0] > 'z')) oktosend = 0;
- if ((dataline[35] != 'K')&&(dataline[35] != 'M')) oktosend = 0;
- if (dataline[21] < '0') oktosend = 0;
- }
-
-
- if (oktosend)
- {
- UpdateByteCount(dataline,1);
- /* Take it out of filelist */
- GT_SetGadgetAttrs(toplist, mgetWnd, NULL, GTLV_Labels, ~0, TAG_END);
- Remove(current);
- GT_SetGadgetAttrs(toplist, mgetWnd, NULL, GTLV_Labels, filelist, TAG_END);
-
- /* And insert it in getlist */
- /* first detach list to modify it */
- GT_SetGadgetAttrs(botlist, mgetWnd, NULL, GTLV_Labels, ~0, TAG_END);
- AddTail(getlist,current);
- GT_SetGadgetAttrs(botlist, mgetWnd, NULL, GTLV_Labels, getlist, TAG_END);
- files_selected++;
- if (!noparse) ChangeWindowTitle();
- }
- }
-
- }
-
- int list2Clicked (VOID)
- {
- /* This function will move a clicked item from the lower list to the upper one */
-
- int i;
- struct Node *current = getlist->lh_Head;
- struct Gadget *toplist = mgetGadgets[0];
- struct Gadget *botlist = mgetGadgets[1];
-
- for (i=0; i<lastcode; i++)
- {
- current = current->ln_Succ;
- }
-
- UpdateByteCount(current->ln_Name,-1);
-
- /* Take it out of getlist */
- GT_SetGadgetAttrs(botlist, mgetWnd, NULL, GTLV_Labels, ~0, TAG_END);
- Remove(current);
- GT_SetGadgetAttrs(botlist, mgetWnd, NULL, GTLV_Labels, getlist, TAG_END);
-
-
- /* And insert it in filelist */
- GT_SetGadgetAttrs(toplist, mgetWnd, NULL, GTLV_Labels, ~0, TAG_END);
- AddTail(filelist,current);
- GT_SetGadgetAttrs(toplist, mgetWnd, NULL, GTLV_Labels, filelist, TAG_END);
- /* and that's it! */
-
- files_selected--;
- if (!noparse) ChangeWindowTitle();
- }
-
- int OutputList(struct List *thislist)
- {
- char *j, *j2;
- int i=0,k,l,z,q,next_bytes;
- int next_splitpoint = MAXOUTPUTLINELENGTH, printed_bytes = 0;
- char outname[80];
- struct Node *current = thislist->lh_Head;
-
- if (!nomget) {printf("mget "); printed_bytes = 5;}
-
- while (current != NULL)
- {
- /* watch out for empty nodes! */
- i++;
-
- if (current->ln_Name != NULL)
- {
- z = strlen(current->ln_Name);
- }
- else
- {
- z=0;
- }
-
- if (z>21)
- {
- j = strchr(current->ln_Name,' '); /* address of first space after filename */
- k = j - current->ln_Name; /* length of filename */
- j2 = strchr(current->ln_Name+21,' '); /* address of directory of file */
- l = j2 - (current->ln_Name) - 21; /* length of directory */
-
- if ((k<22)&&(k>0)&&(l<12)&&(l>2))
- {
- for (q=0;q<80;q++)
- outname[q]=0;
- strncat(outname,current->ln_Name+21,l);
- strncat(outname,"/",1);
- strncat(outname,current->ln_Name,k);
- outname[79]=0; /* just in case, we'll reterminate the string */
- next_bytes = strlen(outname) + 1; /* + 1 for the space */
- if ((next_bytes>3)&&(next_bytes<33))
- {
- if ((!nosplit)&&((printed_bytes + next_bytes) > next_splitpoint))
- {
- if (nomget)
- {
- printf("\r\n");
- printed_bytes = 0;
- }
- else
- {
- printf("\r\nmget ");
- printed_bytes = 5; /* reset counter for next batch */
- }
-
- }
-
- printf("%s ",outname);
- printed_bytes = printed_bytes + next_bytes;
- }
- }
- }
- current = current->ln_Succ;
- }
- return(1);
-
- }
-
- int RawOutputList(struct List *thislist)
- {
- struct Node *current = thislist->lh_Head;
-
- while (current != NULL)
- {
- if ((current->ln_Name != NULL)&&
- (*current->ln_Name != 10)&&
- (*current->ln_Name != 13))
- {
- printf("%.80s\n",current->ln_Name);
- }
- current = current->ln_Succ;
- }
- return(1);
- }
-
- int mgetabout (VOID)
- {
- EasyRequest(NULL, &aboutreq, NULL, "(Variable)", NULL /* was number */);
- }
-
- int mgetquit (VOID)
- {
- done=TRUE;
- }
-
- int mgetcopy (VOID)
- {
- /* printf ("Copy menu item selected 64\n"); */
- }
-
- int mgetcut (VOID)
- {
- /* printf ("Cut menu item selected 65\n"); */
- }
-
- int mgetpaste (void)
- {
- /* printf("Paste menu item selected 66\n"); */
- }
-
- int mgetCloseWindow (void)
- {
- done=TRUE;
- }
-
-
-
- /* Turns all \n's in input string into <space><newline>'s */
- void FindReturns(char *mystring)
- {
- char *thischar=mystring, *prevchar=mystring;
-
- while (*thischar != NULL)
- {
-
- if ( (*prevchar == 92) && /* any '/'s ? */
- ((*thischar == 78)||(*thischar == 110)) && /* N's or n's */
- (prevchar != NULL) && /* Sanity chk */
- (thischar != NULL))
-
- {
- *prevchar = 32; /* set this to a space--easier than shortening string */
- *thischar = 10; /* and end this line with a newline char */
- }
-
- prevchar = thischar;
- thischar++;
- }
- }
-
- int AddFile(struct List *mylist, char *newstring) /* 1 = success, 0 = failure */
- {
- struct Node *newnode;
- char *newname, *i, returnchar = 10, spacechar = 32;
- UBYTE diff=NAMELENGTH;
-
- if (strlen(newstring) < 1) return(0);
-
- /* make sure there are carriage returns in newname! */
-
- if (!( newnode = AllocMem(sizeof(struct Node),MEMF_CLEAR) ))
- {
- printf("Couldn't allocate memory for Node!\n");
- return(0);
- }
-
- i = strchr(newstring, (char) returnchar);
- if (i)
- {
- diff = i-newstring; /* number of chars into the string where newline is! */
- }
-
-
- if (!( newname = AllocMem(diff+1,MEMF_CLEAR) ))
- {
- printf("Couldn't allocate memory for Node String! %i\n",diff);
- return(0);
- }
-
- strncpy(newname,newstring,diff); /* copy over the bytes */
-
- newnode->ln_Pri = diff - 126; /* store string length in unused field */
- /* really bad style, isn't it? */
-
- newnode->ln_Name=newname; /* and set ze pointer! */
-
- AddTail(mylist, newnode);
- return(1);
- }
-
- /* returns the number of lines successfully parsed and added to the list */
- int ReadmyFile(void)
- {
- FILE *input_file;
- char inputline[MAXFILELINELENGTH];
- char *firstnull;
- int i,nullpos = 0, okline = 0;
- int filelength = 0, failed_lines = 0, added_lines = 0;
-
- if (input_file = fopen(inputfile,"r"))
- {
- while (!feof(input_file))
- {
- filelength++;
- okline=0;
- /* Read until we get a line-end */
- while((okline==0)&&(!feof(input_file)))
- {
- for(i=0; ((i<MAXFILELINELENGTH)&&(inputline[i] != 0)); i++)
- {
- inputline[i] = 0;
- }
- fgets(inputline,NAMELENGTH,input_file); /* Read up to 80 chars from inputfile */
- firstnull = strchr(inputline,(char) 10); /* Find address of carriage return */
- nullpos = firstnull - inputline; /* Subtract to get return pos */
- if ((nullpos > 1)&&(nullpos < NAMELENGTH)) okline=1;
- }
- if ((nullpos < NAMELENGTH)||(noparse))
- {
- if (AddFile(filelist, inputline) == 1)
- {
- added_lines++;
- }
- else
- {
- failed_lines++;
- }
- }
- else
- {
- failed_lines++;
- }
-
- }
- }
- else
- {
- printf("Couldn't open file!\n");
- return(0);
- }
-
- fclose(input_file);
- return(added_lines);
- }
-
- VOID FreeFileNodes(struct List *mylist)
- {
- struct Node *nextnode,*worknode = mylist->lh_Head;
- int nodesize = sizeof(struct Node);
-
- while (nextnode = (worknode->ln_Succ))
- {
- if (worknode->ln_Name != NULL)
- {
- FreeMem(worknode->ln_Name,worknode->ln_Pri+127);
- }
-
- FreeMem(worknode,nodesize);
- worknode = nextnode;
- }
- }
-
-
- int ChangeWindowTitle(void)
- {
- sprintf(windowtitle,"%i files, %u kilobytes selected.",files_selected, kbytes_selected);
- SetWindowTitles(mgetWnd, windowtitle , (char *) ~0);
- return(1);
- }
-
-
- VOID UpdateByteCount(char *mystring, int addorsub)
- {
- int multiplier = 0;
- int filesize = 0, wholenum;
- int outputlength = -1;
-
- if (mystring == NULL) {printf("UBC: mystring null!\n"); return(); }
-
- if (mystring[35] == 'M') multiplier = 1024; /* 1024 k in a M */
- if (mystring[35] == 'K') multiplier = 1; /* 1 k in a k */
-
- if (multiplier == 0) {return();} /* If we don't know M or K, abort */
-
- if (mystring[33] == '.')
- {
- /* figure out kilobytes */
- wholenum = (int) (mystring[32] - 48);
-
- if ((wholenum >= 0)&&(wholenum <= 9))
- {
- filesize = filesize + (multiplier * wholenum);
- }
-
- wholenum = (int) (mystring[34] - 48);
- if ((wholenum >= 0)&&(wholenum <= 9))
- {
- filesize = filesize + ((multiplier/10) * wholenum);
- }
- }
- else
- {
- wholenum = (int) (mystring[32] - 48);
- if ((wholenum >= 0)&&(wholenum <= 9))
- {
- filesize = filesize + (100 * multiplier * wholenum);
- }
-
- wholenum = (int) (mystring[33] - 48);
- if ((wholenum >= 0)&&(wholenum <= 9))
- {
- filesize = filesize + ( 10 * multiplier * wholenum);
- }
-
- wholenum = (int) (mystring[34] - 48);
- if ((wholenum >= 0)&&(wholenum <= 9))
- {
- filesize = filesize + ( 1 * multiplier * wholenum);
- }
- }
-
- kbytes_selected = kbytes_selected + (filesize * addorsub);
- return();
-
- }
-
-
- VOID Cleanup(void)
- {
- if (windowisopen) ClosemgetWindow();
- if (screenisopen) CloseDownScreen();
- if (filelist) {
- FreeFileNodes(filelist);
- FreeMem(filelist,sizeof(struct List));
- }
- if (getlist) {
- FreeFileNodes(getlist);
- FreeMem(getlist,sizeof(struct List));
- }
- if (IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
- if (UtilityBase) CloseLibrary((struct Library *)UtilityBase);
- if (GraphicsBase) CloseLibrary((struct GraphicsLib *)GraphicsBase);
- if (GadToolsBase) CloseLibrary((struct Library *)GadToolsBase);
- return();
- }
-
-
- /********/
- /* MAIN */
- /********/
- VOID main(int argc, char *argv[])
- {
-
- /* if argc=1 then no arguments. If argc =2 then one arg, etc. */
- if (argc==0)
- {
- return();
- }
-
- inputfile[0] = 0;
- if (argc==1)
- {
- printf("Template: GadMGet FILE/S NOPARSE/K NOMGET/K NOSPLIT/K\n");
- return();
- }
-
- /* by here, argc > 1 */
- strncpy(inputfile,argv[1],50);
- if (*inputfile == '?')
- {
- printf("Template: GadMGet FILE/S NOPARSE/K NOMGET/K NOSPLIT/K\n");
- return();
- }
-
- for (int i=3; i<=argc; i++)
- {
- if ((strncmp(argv[i-1],"NOPARSE",7) == 0)||
- (strncmp(argv[i-1],"noparse",7) == 0)||
- (strncmp(argv[i-1],"Noparse",7) == 0)||
- (strncmp(argv[i-1],"NoParse",7) == 0)) noparse = 1;
-
- if ((strncmp(argv[i-1],"NOMGET",6) == 0)||
- (strncmp(argv[i-1],"nomget",6) == 0)||
- (strncmp(argv[i-1],"Nomget",6) == 0)||
- (strncmp(argv[i-1],"NoMget",6) == 0)) nomget = 1;
-
- if ((strncmp(argv[i-1],"NOSPLIT",7) == 0)||
- (strncmp(argv[i-1],"nosplit",7) == 0)||
- (strncmp(argv[i-1],"Nosplit",7) == 0)||
- (strncmp(argv[i-1],"NoSplit",7) == 0)) nosplit = 1;
- }
-
- GraphicsBase = OpenLibrary("graphics.library",37);
- if (GraphicsBase==NULL)
- {
- printf("couldn't open Graphics.library v37!");
- return;
- }
- UtilityBase = OpenLibrary("utility.library",37);
- if (UtilityBase==NULL)
- {
- printf("couldn't open utility.library v37!");
- return;
- }
- GadToolsBase = OpenLibrary("gadtools.library",37);
- if (GadToolsBase==NULL)
- {
- printf("couldn't open gadtools.library v37!\n");
- return;
- }
- IntuitionBase = OpenLibrary("intuition.library",37);
- if (IntuitionBase)
- {
- /* Now add data to listView */
- /* First, initialize List filelist */
-
- if (!(filelist = AllocMem(sizeof(struct List),MEMF_CLEAR)) )
- {
- printf("Couldn't create filelist! Out of memory?\n");
- Cleanup();
- return();
- }
- if (!(getlist = AllocMem(sizeof(struct List),MEMF_CLEAR)) )
- {
- printf("Couldn't create getlist! Out of memory?\n");
- Cleanup();
- return();
- }
-
- NewList(filelist);
- NewList(getlist);
-
- lockscrnerror = SetupScreen();
- if (lockscrnerror)
- {
- printf("couldn't open screen!\n");
- Cleanup();
- return();
- }
- screenisopen = 1;
-
- winopenerror = OpenmgetWindow();
- if (winopenerror)
- {
- printf("Couldn't open window!\n");
- Cleanup();
- return();
- }
- windowisopen = 1;
-
- if (ReadmyFile() > 0)
- {
- /* Put list into top list */
- GT_SetGadgetAttrs((struct Gadget *) mgetGadgets[0], mgetWnd, NULL, GTLV_Labels, filelist, TAG_END);
- files_selected = 0;
-
- if (noparse) SetWindowTitles(mgetWnd, "Please select desired lines now." , (char *) ~0);
- else ChangeWindowTitle();
-
- while (done==FALSE)
- {
- Wait(1L << mgetWnd->UserPort->mp_SigBit);
- handlereturn = HandlemgetIDCMP();
- }
- if (files_selected > 0)
- {
- if (noparse) RawOutputList(getlist);
- else OutputList(getlist);
- }
- }
- }
- Cleanup();
- return();
- }
-
- #endif
-
-
-
-